<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div>
          <el-button icon="el-icon-arrow-left" type="info" size="mini" @click="$router.go(-1)">返回</el-button>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form ref="RoleForm" :rules="rules" :model="RoleForm" label-width="100px" label-position="top" :inline="true"
          size="medium">
          <input type="hidden" v-model="RoleForm.pk_Name" />
          <el-form-item label="名称" prop="RoleName_CHS">
            <el-input placeholder="" v-model="RoleForm.RoleName_CHS"></el-input>
          </el-form-item>
          <el-form-item label="权限值" prop="PowerValue">
            <el-input placeholder="" v-model.number="RoleForm.PowerValue"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" icon="el-icon-check" @click="submit('RoleForm')">提交</el-button>
            <el-button type="normal" size="small" icon="el-icon-close" @click="$router.go(-1)">取消</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<style lang="scss" scoped>
.el-row {
  margin-bottom: 10px;
}

.el-form-item {
  width: 49%;
}
</style>
<script>
export default {
  data () {
    return {
      RoleForm: {
        RoleName_CHT: null,
        RoleName_EN: null,
        fk_Company: null
      },
      Type: 'Add',
      //表单验证
      rules: {
        RoleName_CHS: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        PowerValue: [
          { required: true, message: '权限值不能为空', trigger: 'blur' },
          { type: 'integer', message: '只能输入整数', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //提交表单,添加角色
    submit (RoleForm) {
      //表单验证
      this.$refs[RoleForm].validate((valid) => {
        if (!valid) {
          return false;
        }
        //执行请求
        this.$axios({
          url: '/HrApi/RoleMange/UpdateRoleName',
          method: "POST",
          data: this.$Qs.stringify({
            Role: JSON.stringify(this.RoleForm),
            Type: this.Type
          })
        }).then(res => {
          if (!res.data.code) {
            if (this.Type === "Add") {
              //添加成功，重置表单
              this.$refs[RoleForm].resetFields();
            }
            this.$message.success(res.data.msg)
          } else {
            this.$message.error(res.data.msg)
          }
        })
      })
    }
  },
  mounted () {
    if (Object.keys(this.$route.query).length) {
      //编辑
      this.Type = "Edit";
      this.$axios({
        url: '/HrApi/RoleMange/GetRoleName',
        method: 'GET',
        params: {
          NameID: this.$route.query["pk_Name"],
          Type: "SelID"
        }
      }).then(res => {
        if (!res.data.code) {
          this.RoleForm = res.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      })
    }
  }
}
</script>